<script>
	// #ifdef APP-PLUS
	import APPUpdate from '@/uni_modules/zhouWei-uniAppDemo/js_sdk/appUpdate.js';
	// #endif
	
	export default {
		onLaunch: function() {
			console.log('App Launch')
			 // #ifdef APP-PLUS
			 
			   	this.checkUpdate1();
			    // #endif
				
				
				uni.preloadPage({url: "/pages/index/add"});
				uni.preloadPage({url: "/pages/index/edit"});
				uni.preloadPage({url: "/pages/index/select"});
		},
		onShow: function() {
		
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			checkUpdate1() {
				// 更新版本接口
				var that = this;
				// #ifdef APP-PLUS
				// 获取manifest.json 的信息
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo)=> {
						
					 let curVersion = wgtinfo.version;
					
					this.$api.request('/api/index/versions', {
						curVersion: curVersion,
					},  uni.getStorageSync('token'), function(res){
								console.log(wgtinfo.versionCode,res.data.version,77777);
								
						if (res.data.version > wgtinfo.versionCode) {
							uni.setStorageSync('__package_info__', res.data);
							uni.navigateTo({
								url: `/pages/upgrade-popup/upgrade-popup?local_storage_key=__package_info__`,
								fail: (err) => {
									console.error('更新弹框跳转失败', err);
									uni.removeStorageSync('__package_info__');
								}
							});
							 // uni.showModal({
								// showCancel:res.data.forbit == 1 ? false : true,
								// title: '版本更新',
								// content: res.data.content,
								// confirmText: '更新',
								// success: (e)=> {
								// 	if (e.confirm) {
								// 		if (plus.os.name.toLowerCase() == 'ios') {
								// 			// 跳转到下载页面
								// 			plus.runtime.openURL(res.data.iosurl);
			
								// 		} else {
								// 			that.createDownload(res.data.andriod_file);
								// 		}
								// 	}
								// }
							 // });
						}
					});
			
				});
				// #endif
				// setTimeout(() => {
				// 	uni.navigateTo({
				// 		url: '/pages/upgrade-popup/upgrade-popup?local_storage_key=__package_info__'
				// 	});
				// },3000);
			},
			checkUpdate() {
				// 更新版本接口
				var that = this;
			
			
			
				// #ifdef APP-PLUS
				// 获取manifest.json 的信息
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo)=> {
					 let curVersion = wgtinfo.version;
					console.log(curVersion);
					this.$api.request('/api/index/versions', {
						curVersion: curVersion,
					}, this.$store.state.token, function(res){
						console.log(res);
						console.log(res.data.version ,wgtinfo.versionCode);
						if (res.data.version > wgtinfo.versionCode) {
							 uni.showModal({
								showCancel:res.data.forbit == 1 ? false : true,
								title: '版本更新',
								content: res.data.content,
								confirmText: '更新',
								success: (e)=> {
									if (e.confirm) {
										if (plus.os.name.toLowerCase() == 'ios') {
											// 跳转到下载页面
											plus.runtime.openURL(res.data.iosurl);
			
										} else {
											that.createDownload(res.data.andriod_file);
										}
									}
								}
							 });
						}
					});
			
				});
				// #endif
			},
			createDownload(url) {
				console.log(url,88888);
				var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
					uni.showToast({
						title: '下载完成',
						mask: false,
						duration: 1000
					});
					// 下载完成
					if (status == 200) {
						plus.runtime.install(
							plus.io.convertLocalFileSystemURL(d.filename),
							{},
							function(success) {
								uni.showToast({
									title: '安装成功',
									mask: false,
									duration: 1500
								});
							},
							function(error) {
								uni.showToast({
									title: '安装失败-01',
									mask: false,
									duration: 1500
								});
							}
						);
					} else {
						uni.showToast({
							title: '更新失败-02',
							mask: false,
							duration: 1500
						});
					}
				});
				try {
					dtask.start(); // 开启下载的任务
					var prg = 0;
					var showLoading = plus.nativeUI.showWaiting('正在下载'); //创建一个showWaiting对象
			
					dtask.addEventListener('statechanged', function(task, status) {
						// 给下载任务设置一个监听 并根据状态  做操作
						switch (task.state) {
							case 1:
								showLoading.setTitle('正在下载');
								break;
							case 2:
								showLoading.setTitle('已连接到服务器');
								break;
							case 3:
								prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
								showLoading.setTitle('  正在下载' + prg + '%  ');
								break;
							case 4:
								plus.nativeUI.closeWaiting();
								//下载完成
								break;
						}
					});
				} catch (err) {
					plus.nativeUI.closeWaiting();
					uni.showToast({
						title: '更新失败-03',
						mask: false,
						duration: 1500
					});
				}
			}
		},
	}
</script>



<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uview-ui/index.scss';
@import "moyi-ui/main.scss";
page {
	background: #f5f6fa;
}
button::after {
	border: none;
}
*{
	vertical-align: top;
	box-sizing: border-box;
}
</style>
<style lang="scss">
	@import "color-ui/main.css";
	@import "color-ui/animation.css";
	@import "color-ui/icon.css";
</style>



